unit UPesquisaPedidoVenda;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, UFormPesquisa, DB, DBTables, StdCtrls, Grids, DBGrids, Buttons,
  ExtCtrls, DBCtrls, Mask, ToolEdit;

type
  TFNovaPesquisaPedidoVenda = class(TFormPesquisa)
    EdCodigo: TEdit;
    Label1: TLabel;
    GBData: TGroupBox;
    Label2: TLabel;
    DTData1: TDateEdit;
    Label3: TLabel;
    DTData2: TDateEdit;
    RBSituacao: TRadioGroup;
    Label5: TLabel;
    DBLCBCliente: TDBLookupComboBox;
    Label13: TLabel;
    DBLCBVendedor: TDBLookupComboBox;
    Label32: TLabel;
    DBLLocalBaixaEstoquePed: TDBLookupComboBox;
    Label11: TLabel;
    DBLCBTipoVenda: TDBLookupComboBox;
    DSLocal: TDataSource;
    DSVendedor: TDataSource;
    DSTipoVenda: TDataSource;
    DSCliente: TDataSource;
    QPesquisaPED_CODIGO: TIntegerField;
    QPesquisaDATA: TDateTimeField;
    QPesquisaVALOR: TFloatField;
    QPesquisaQTD_ITENS: TIntegerField;
    QPesquisaCLIENTE: TStringField;
    QTipoVenda: TQuery;
    Label4: TLabel;
    Label6: TLabel;
    EdNTalao: TEdit;
    procedure DBLCBClienteKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure DBLCBVendedorKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure DBLLocalBaixaEstoquePedKeyDown(Sender: TObject;
      var Key: Word; Shift: TShiftState);
    procedure DBLCBTipoVendaKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure BtnConsultarClick(Sender: TObject);
    procedure DTData1Exit(Sender: TObject);
    procedure BtnSelecionarClick(Sender: TObject);
    procedure BtnLimparClick(Sender: TObject);
    procedure DBGrid1DblClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FNovaPesquisaPedidoVenda: TFNovaPesquisaPedidoVenda;

implementation

uses UGeral, UGestao, UDMGeral, UDmGestao, UDmFinanceiro, PedidoVenda_NOVO,
  UPesquisaRapida;

{$R *.dfm}

procedure TFNovaPesquisaPedidoVenda.DBLCBClienteKeyDown(Sender: TObject;
  var Key: Word; Shift: TShiftState);
begin
  inherited;

  IF KEY = VK_F8 THEN
  BEGIN

    Application.CreateForm(TFPesquisaRapida,FPesquisaRapida);

    FPesquisaRapida.tipo := 'CLIENTE';
    CaPtUrA_pEsQuIsA_rApIdA := '';

    FPesquisaRapida.ShowModal;

    if CaPtUrA_pEsQuIsA_rApIdA <> '' then
      DBLCBCliente.KeyValue := CaPtUrA_pEsQuIsA_rApIdA;

    CaPtUrA_pEsQuIsA_rApIdA := '';

    Exit;

  END;

  case key of
  114 : ShowMessage(TDBLookupComboBox(Sender).Text);

  116 : begin
           TDBLookupComboBox(Sender).ListSource.DataSet.Close;
           TDBLookupComboBox(Sender).ListSource.DataSet.Open;
          end ;
 end;


 if key = 46 then
   TDBLookupComboBox(Sender).KeyValue := null;

end;

procedure TFNovaPesquisaPedidoVenda.DBLCBVendedorKeyDown(Sender: TObject;
  var Key: Word; Shift: TShiftState);
begin
  inherited;
  case key of
  114 : ShowMessage(TDBLookupComboBox(Sender).Text);

  116 : begin
           TDBLookupComboBox(Sender).ListSource.DataSet.Close;
           TDBLookupComboBox(Sender).ListSource.DataSet.Open;
          end ;
 end;


 if key = 46 then
  begin
   TDBLookupComboBox(Sender).KeyValue:=null;
  // TDBLookupComboBox(Sender).DataSource.DataSet.FieldByName(TDBLookupComboBox(Sender).DataField).AsVariant:=null;
  end;

end;

procedure TFNovaPesquisaPedidoVenda.DBLLocalBaixaEstoquePedKeyDown(Sender: TObject;
  var Key: Word; Shift: TShiftState);
begin
  inherited;
  case key of
  114 : ShowMessage(TDBLookupComboBox(Sender).Text);

  116 : begin
           TDBLookupComboBox(Sender).ListSource.DataSet.Close;
           TDBLookupComboBox(Sender).ListSource.DataSet.Open;
          end ;
 end;


 if key = 46 then
  begin
   TDBLookupComboBox(Sender).KeyValue:=null;
 //  TDBLookupComboBox(Sender).DataSource.DataSet.FieldByName(TDBLookupComboBox(Sender).DataField).AsVariant:=null;
  end;

end;

procedure TFNovaPesquisaPedidoVenda.DBLCBTipoVendaKeyDown(Sender: TObject;
  var Key: Word; Shift: TShiftState);
begin
  inherited;
  case key of
  114 : ShowMessage(TDBLookupComboBox(Sender).Text);

  116 : begin
           TDBLookupComboBox(Sender).ListSource.DataSet.Close;
           TDBLookupComboBox(Sender).ListSource.DataSet.Open;
          end ;
 end;


 if key = 46 then
  begin
   TDBLookupComboBox(Sender).KeyValue:=null;
 //  TDBLookupComboBox(Sender).DataSource.DataSet.FieldByName(TDBLookupComboBox(Sender).DataField).AsVariant:=null;
  end;

end;

procedure TFNovaPesquisaPedidoVenda.BtnConsultarClick(Sender: TObject);
const Consulta_pedido_Venda = ' SELECT                                                                      '+#10+
                              '         PEDIDOVENDA.PED_CODIGO,                                             '+#10+
                              '         PEDIDOVENDA.PED_DATA AS DATA,                                       '+#10+
                              '         CLIENTE.CLI_RAZAO AS CLIENTE,                                       '+#10+
                              '         SUM(ITENSPEDIDOVENDA.ITENSPED_VALORTOTAL) AS VALOR,                 '+#10+
                              '         COUNT(ITENSPEDIDOVENDA.ITENSPED_VALORTOTAL) AS QTD_ITENS            '+#10+

                              ' FROM PEDIDOVENDA                                                            '+#10+

                              '         INNER JOIN ITENSPEDIDOVENDA ON                                      '+#10+
                              '                 (ITENSPEDIDOVENDA.ITENSPED_PEDIDO = PEDIDOVENDA.PED_CODIGO) '+#10+

                              '         INNER JOIN CLIENTE ON                                               '+#10+
                              '                 (CLIENTE.CLI_CODIGO = PEDIDOVENDA.PED_CLIENTE)              '+#10+

                              ' WHERE (PEDIDOVENDA.PED_CODIGO > 0) %s                                       '+#10+

                              ' GROUP BY                                                                    '+#10+
                              '         PEDIDOVENDA.PED_CODIGO,                                             '+#10+
                              '         PEDIDOVENDA.PED_DATA,                                               '+#10+
                              '         CLIENTE.CLI_RAZAO                                                   '+#10+

                              ' ORDER BY                                                                    '+#10+
                              '         PEDIDOVENDA.PED_CODIGO                                              ';
VAR Filtro : String;
begin

  Filtro := '';

  if trim(EdCodigo.Text) <> '' then
    Filtro := ' AND (PEDIDOVENDA.PED_CODIGO = ' +EdCodigo.Text+ ') ';

  if trim(EdNTalao.Text) <> '' then
    Filtro := ' AND (PEDIDOVENDA.PED_NUMERO_TALAO = ' +EdNTalao.Text+ ') ';


  if (DTData1.Date <> 0) and (DTData2.Date <> 0) then
    Filtro := Filtro + ' AND (PEDIDOVENDA.PED_DATA BETWEEN ' +QuotedStr(FormatDateTime('mm/dd/yyyy 00:00:00',DTData1.Date))+ ' AND '+
                                                              QuotedStr(FormatDateTime('mm/dd/yyyy 23:59:59',DTData2.Date))+') ';
  CASE RBSituacao.ItemIndex of
    0: Filtro := Filtro + ' AND (PEDIDOVENDA.PED_SITUACAO = 0) ';
    1: Filtro := Filtro + ' AND (PEDIDOVENDA.PED_SITUACAO = 1) ';
    2: Filtro := Filtro + ' AND (PEDIDOVENDA.PED_SITUACAO = 2) ';
    3: Filtro := Filtro;
  END;

  IF DBLCBCliente.KeyValue <> Null then
    Filtro := Filtro + ' AND (PEDIDOVENDA.PED_CLIENTE = '+ VarToStr(DBLCBCliente.KeyValue) +') ';

  IF DBLCBVendedor.KeyValue <> Null then
    Filtro := Filtro + ' AND (PEDIDOVENDA.PED_VENDEDOR = '+ VarToStr(DBLCBVendedor.KeyValue) +') ';

  IF DBLLocalBaixaEstoquePed.KeyValue <> Null then
    Filtro := Filtro + ' AND (PEDIDOVENDA.PED_LOCAL = '+ VarToStr(DBLLocalBaixaEstoquePed.KeyValue) +') ';

  IF DBLCBTipoVenda.KeyValue <> Null then
    Filtro := Filtro + ' AND (PEDIDOVENDA.PED_TIP_VENDA = '+ VarToStr(DBLCBTipoVenda.KeyValue) +') ';

  QPesquisa.Close;
  QPesquisa.SQL.Clear;
  QPesquisa.SQL.Add(Format(Consulta_pedido_Venda,[filtro]));
  QPesquisa.Open;

  inherited;

end;

procedure TFNovaPesquisaPedidoVenda.DTData1Exit(Sender: TObject);
begin
  inherited;
  DTData2.Date := DTData1.Date;
end;

procedure TFNovaPesquisaPedidoVenda.BtnSelecionarClick(Sender: TObject);
begin

  IF NOT(DsPesquisa.DataSet.IsEmpty) THEN
  BEGIN
    FPedidoVenda_NOVO.dsCadastro.DataSet.Locate('PED_CODIGO',DsPesquisa.DataSet.Fieldbyname('PED_CODIGO').AsString,[]);
    BtnFecharClick(sENDER);
    inherited;
    EXIT;
  eND;

end;

procedure TFNovaPesquisaPedidoVenda.BtnLimparClick(Sender: TObject);
begin
  inherited;
  
  EdCodigo.Clear;

  DTData1.Date := 0;
  DTData2.Date := 0;

  RBSituacao.ItemIndex := 3;

  DBLCBCliente.KeyValue            := Null;
  DBLCBVendedor.KeyValue           := Null;
  DBLLocalBaixaEstoquePed.KeyValue := Null;
  DBLCBTipoVenda.KeyValue          := Null;


end;

procedure TFNovaPesquisaPedidoVenda.DBGrid1DblClick(Sender: TObject);
begin
  inherited;
  if BtnSelecionar.Enabled then
    BtnSelecionar.Click;
end;

end.
